/*
 * @Author: your name
 * @Date: 2022-03-19 19:33:41
 * @LastEditTime: 2022-03-26 23:41:44
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \driving\src\article\entities\article.entity.ts
 */
import { Exclude } from "class-transformer";
import { CategoryEntity } from "src/category/entities/category.entity";
import { User } from "src/user/entities/user.entity";
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { ArticleInfoDto } from "../dto/create-article.dto";
@Entity('article')
export class ArticleEntity {
  @PrimaryGeneratedColumn() // 标记为主列
  id:number;

  // 文章标题
  @Column({ length:50})
  title:string;

  // markdown内容
  @Column({ type:'mediumtext',default:null})
  content:string;

  // markdown 转html,自动生成
  @Column({ type:'mediumtext',default:null,name:'content_html'})
  contentHtml:string;

  // 摘要，自动生成
  @Column({type:'text',default:null})
  summary:string;

  // 封面图
  @Column({default:null,name:'cover_url'})
  coverUrl:string;

  // 阅读量
  @Column({type:'int',default:0})
  count:number

  // 点赞数
  @Column({type:'int',default:0,name:'like_count'})
  likeCount:number;

  // 推荐显示
  @Column({type:'tinyint',default:0,name:'is_recommend'})
  isRecommend:number;

  // 文章状态
  @Column('simple-enum',{enum:['draft','publish'],default:'draft'})
  status:string

  // 作者
  @ManyToOne((type) => User,(user) => user.username)
  author:User;
  // 分类
  @Exclude()
  @ManyToOne(() => CategoryEntity,(category) => category.categorys)
  @JoinColumn({
    name:'category_id',
  })
  category:CategoryEntity;

  @Column({type:'timestamp',name:'publish_time',default:null})
  publishTime:Date;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  create_time: Date;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  update_time: Date;

  toResponseObject(): ArticleInfoDto {
    let responseObj: ArticleInfoDto = {
      ...this,
      isRecommend: this.isRecommend ? true : false,
    };
    if (this.category) {
      responseObj.category = this.category.name;
    }
    if (this.author && this.author.id) {
      responseObj.userId = String(this.author.id) ;
      responseObj.author = this.author.nickname || this.author.username;
    }
    return responseObj;
  }
}
